package com.jlnk.androidfmc;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.androidfmc.adapter.SettingAdapter;
import com.jlnk.androidfmc.entity.SettingItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SettingsActivity extends AppCompatActivity {

  private SettingAdapter adapterProfile, adapterPwd, adapterService;
  private Toolbar toolbar;
  private RecyclerView rclProfile, rclPwd, rclService;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EdgeToEdge.enable(this);
    setContentView(R.layout.activity_settings);
    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
      Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
      v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
      return insets;
    });
    Tools.setSystemBarColorInt(this, Color.parseColor("#FFFFFF"));
    init();
  }

  private void init() {
    initView();
  }

  private void initView() {
    toolbar = findViewById(R.id.toolbar);
    rclProfile = findViewById(R.id.rcl_profile);
    rclPwd = findViewById(R.id.rcl_pwd);
    rclService = findViewById(R.id.rcl_service);
    initToolbar();
    initRcl();
  }

  private void initToolbar() {
    setSupportActionBar(toolbar);
    Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
  }

  private void initRcl() {
    List<SettingItem> itemsProfile = new ArrayList<>();
    List<SettingItem> itemsPwd = new ArrayList<>();
    List<SettingItem> itemsService = new ArrayList<>();

    String[] itemsProfileTitles = {"个人信息", "实名认证", "专家认证", "地址管理", "隐私设置"};
    String[] itemsPwdTitles = {"登录密码", "提现密码"};
    String[] itemsServiceTitles = {"长辈模式", "联系客服", "意见反馈", "关于我们", "隐私政策",
        "用户协议",
        "推荐朋友", "缓存数据", "检测更新", "版本信息"};

    for (String title : itemsProfileTitles) {
      String id = null;
      String actionDesc = null;
      if (title.equals("地址管理")) {
        id = "manage_address";
      }
      if (title.equals("实名认证")) {
        actionDesc = "未实名";
      }
      itemsProfile.add(new SettingItem(id, title, actionDesc));
    }
    for (String title : itemsPwdTitles) {
      itemsPwd.add(new SettingItem(null, title, "修改"));
    }
    for (String title : itemsServiceTitles) {
      String actionDesc = null;
      switch (title) {
        case "长辈模式":
          actionDesc = "未开启";
          break;
        case "缓存数据":
          actionDesc = "11.45mb 待清理";
          break;
        case "版本信息":
          actionDesc = "V1.0.0";
          break;
      }
      itemsService.add(new SettingItem(null, title, actionDesc));
    }

    rclProfile.setLayoutManager(new LinearLayoutManager(this));
    rclPwd.setLayoutManager(new LinearLayoutManager(this));
    rclService.setLayoutManager(new LinearLayoutManager(this));

    adapterProfile = new SettingAdapter(
        item -> {
          if (item.getId() == null) {
            return;
          }
          if (item.getId().equals("manage_address")) {
            startActivity(new Intent(SettingsActivity.this, AddressActivity.class));
          }
        }
    );

    adapterPwd = new SettingAdapter(item -> {
    });
    adapterService = new SettingAdapter(item -> {
    });

    adapterProfile.insertAllItems(itemsProfile);
    adapterPwd.insertAllItems(itemsPwd);
    adapterService.insertAllItems(itemsService);

    rclProfile.setAdapter(adapterProfile);
    rclPwd.setAdapter(adapterPwd);
    rclService.setAdapter(adapterService);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
    }
    return super.onOptionsItemSelected(item);
  }

}